package com.yonyou.pmclouds.plan.processor;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.plan.entity.ScheduleItemVO;
import com.yonyou.pmclouds.plan.plandriven.entity.PlanDrivenParamVO;
import com.yonyou.pmclouds.plan.plandriven.rmiitf.IScheduleDriven;

/**
 * 友工程单据审批后，驱动计划完成（目前只支持开工报告、综合验收,阶段验收）
 *
 * @author th
 * @date 2019/11/22 15:37
 */
public class ScheduleDrivenProcessor<T extends SuperVO> implements SingleProcessor<T> {
    private IScheduleDriven scheduleDriven;

    public ScheduleDrivenProcessor(IScheduleDriven scheduleDriven) {
        this.scheduleDriven = scheduleDriven;
    }

    @Override
    public void process(T vo, T orig) throws BusinessException {
        String billType = BeanHelper.getProperty(vo, CommonFieldConst.BILL_TYPE) == null ?
                BeanHelper.getProperty(vo, CommonFieldConst.BILLTYPE).toString() : BeanHelper.getProperty(vo, CommonFieldConst.BILL_TYPE).toString();
        if (!BillTypeConst.WORKSTART_BILLTYPE.equals(billType) && !BillTypeConst.COMPREHENSIVE_ACCEPTANCE_BILLTYPE.equals(billType) &&
                !BillTypeConst.STAGECHECK_BILLTYPE.equals(billType))
            return;

        String pkBill = vo.pkFiledValue() == null ? null : vo.pkFiledValue().toString();
        String pkProject = BeanHelper.getProperty(vo, CommonFieldConst.PKPROJECT) == null ?
                BeanHelper.getProperty(vo, CommonFieldConst.PK_PROJECT).toString() : BeanHelper.getProperty(vo, CommonFieldConst.PKPROJECT).toString();

        PlanDrivenParamVO paramVO = new PlanDrivenParamVO();
        paramVO.setPkBill(pkBill);
        paramVO.setPkProject(pkProject);
        paramVO.setSrcType(PlanDrivenParamVO.SRC_PMC);
        paramVO.setBillType(billType);
        paramVO.setUserId(BeanHelper.getProperty(vo, CommonFieldConst.CREATOR).toString());
        // 目前，友工程的单据只支持驱动，不支持回滚
        paramVO.setAction(PlanDrivenParamVO.ACTION_DRIVE);
        paramVO.setPkScheduleItem(BeanHelper.getProperty(vo, ScheduleItemVO.PK_SCHEDULE_ITEM) == null ?
                                       "" : BeanHelper.getProperty(vo, ScheduleItemVO.PK_SCHEDULE_ITEM).toString());

        // 驱动计划
        scheduleDriven.drivePlanByMilestoneBill(paramVO);
    }
}
